﻿using System;
using fastCSharp;
using System.Threading;
using System.Reflection;

namespace diantou.web
{
    class Program
    {
        /// <summary>
        /// Http服务客户端
        /// </summary>
        private static fastCSharp.net.tcp.http.servers.tcpClient httpClient;
        /// <summary>
        /// 静态文件域名
        /// </summary>
        private static fastCSharp.net.tcp.http.domain fileServerDomain = fastCSharp.config.pub.LoadConfig(new fastCSharp.net.tcp.http.domain(), "file");
        /// <summary>
        /// 图片文件域名
        /// </summary>
        private static fastCSharp.net.tcp.http.domain imageServerDomain = fastCSharp.config.pub.LoadConfig(new fastCSharp.net.tcp.http.domain(), "img");
        /// <summary>
        /// 考试图片文件域名
        /// </summary>
        private static fastCSharp.net.tcp.http.domain examImageServerDomain = fastCSharp.config.pub.LoadConfig(new fastCSharp.net.tcp.http.domain(), "photo");
        /// <summary>
        /// web视图域名
        /// </summary>
        private static fastCSharp.net.tcp.http.domain webServerDomain = fastCSharp.config.pub.LoadConfig(new fastCSharp.net.tcp.http.domain(), "www");
        /// <summary>
        /// 考试web视图域名
        /// </summary>
        private static fastCSharp.net.tcp.http.domain examServerDomain = fastCSharp.config.pub.LoadConfig(new fastCSharp.net.tcp.http.domain(), "exam");
        /// <summary>
        /// 轮询域名
        /// </summary>
        private static fastCSharp.net.tcp.http.domain pollServerDomain = fastCSharp.config.pub.LoadConfig(new fastCSharp.net.tcp.http.domain(), "poll");
        /// <summary>
        /// 重定向域名集合
        /// </summary>
        private static fastCSharp.net.tcp.http.domain[] locationWebServerDomain;

        private unsafe static void Main(string[] args)
        {
            if (fastCSharp.config.appSetting.IsCheckMemory) checkMemory.Add(typeof(Program));
            locationWebServerDomain = diantou.dataModel.path.domain.Default.LocationDomains.getArray(value => new fastCSharp.net.tcp.http.domain { Domain = value, Host = webServerDomain.Host, SslHost = webServerDomain.SslHost });
            while (true)
            {
                try
                {
                    using (httpClient = new fastCSharp.net.tcp.http.servers.tcpClient())
                    {
                        using (fastCSharp.threading.task task = new fastCSharp.threading.task(4))
                        {
                            httpClient.stop(new fastCSharp.net.tcp.http.domain[] { webServerDomain, pollServerDomain, examServerDomain });
                            task.Add(web);
                            httpClient.stop(fileServerDomain);
                            task.Add(file);
                            httpClient.stop(new fastCSharp.net.tcp.http.domain[] { imageServerDomain, examImageServerDomain });
                            task.Add(image);
                            httpClient.stop(locationWebServerDomain);
                            task.Add(locationWeb);
                        }
                        //output("Finally");//主线程有问题(VS中不存在该问题，直接运行有问题)
                        Console.WriteLine("Finally");
                        return;
                    }
                }
                catch (Exception error)
                {
                    output(error);
                }
                Thread.Sleep(1000);
            }
        }
        /// <summary>
        /// 输出错误信息
        /// </summary>
        /// <param name="error"></param>
        private static void output(Exception error)
        {
            Console.WriteLine(error.ToString());
            log.Default.Real(error, null, false);
        }
        /// <summary>
        /// 输出信息
        /// </summary>
        /// <param name="message"></param>
        private static void output(string message)
        {
            Console.WriteLine(message);
            log.Default.Real(message, new System.Diagnostics.StackFrame(), true);
        }
        /// <summary>
        /// 启动静态文件域名
        /// </summary>
        private static void file()
        {
            while (true)
            {
                try
                {
                    fastCSharp.net.tcp.http.servers.startState state = httpClient.start(Assembly.GetExecutingAssembly().Location, typeof(server.staticFileServer).FullName, fileServerDomain, true);
                    output("staticFileServer " + state.ToString());
                    if (state == fastCSharp.net.tcp.http.servers.startState.Success) return;
                }
                catch (Exception error)
                {
                    output(error);
                }
                Thread.Sleep(1000);
            }
        }
        /// <summary>
        /// 启动图片文件域名
        /// </summary>
        private static void image()
        {
            while (true)
            {
                try
                {
                    fastCSharp.net.tcp.http.servers.startState state = httpClient.start(Assembly.GetExecutingAssembly().Location, typeof(server.imageServer).FullName, new fastCSharp.net.tcp.http.domain[] { imageServerDomain, examImageServerDomain }, true);
                    output("imageServer " + state.ToString());
                    if (state == fastCSharp.net.tcp.http.servers.startState.Success) return;
                }
                catch (Exception error)
                {
                    output(error);
                }
                Thread.Sleep(1000);
            }
        }
        /// <summary>
        /// 启动web视图域名+轮询域名+考试域名
        /// </summary>
        private static void web()
        {
            while (true)
            {
                try
                {
                    fastCSharp.net.tcp.http.servers.startState state = httpClient.start(Assembly.GetExecutingAssembly().Location, typeof(webServer).FullName, new fastCSharp.net.tcp.http.domain[] { webServerDomain, pollServerDomain, examServerDomain }, true);
                    output("webServer " + state.ToString());
                    if (state == fastCSharp.net.tcp.http.servers.startState.Success) return;
                }
                catch (Exception error)
                {
                    output(error);
                }
                Thread.Sleep(1000);
            }
        }
        /// <summary>
        /// 启动重定向域名集合
        /// </summary>
        private static void locationWeb()
        {
            if (locationWebServerDomain.length() != 0)
            {
                while (true)
                {
                    try
                    {
                        fastCSharp.net.tcp.http.servers.startState state = httpClient.start(Assembly.GetExecutingAssembly().Location, typeof(server.locationServer).FullName, locationWebServerDomain, true);
                        output("locationServer " + state.ToString());
                        if (state == fastCSharp.net.tcp.http.servers.startState.Success) return;
                    }
                    catch (Exception error)
                    {
                        output(error);
                    }
                    Thread.Sleep(1000);
                }
            }
        }
    }
}
